<?php
/*
# ------------------------------------------------------------------------
# JA Seleni for Joomla 1.5.x - Version 1.0 - Licence Owner JA115884
# ------------------------------------------------------------------------
# Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: J.O.O.M Solutions Co., Ltd
# Websites:  http://www.joomlart.com -  http://www.joomlancers.com
# This file may not be redistributed in whole or significant part.
# ------------------------------------------------------------------------
*/

class CObjectExtendable
{
	// extended objects
	var $_aExtended =     array();
	
	// add extension
	function _extend($oObject)
	{
	/*
		if (is_object($oObject)) {
			$this->_aExtended[] = $oObject;
		} else if (is_array($oOject)) {
			$this->_aExtended = array_merge($this->_aExtended, $oObject);
		}
	*/	
		$this->_aExtended = $oObject;
	}
    	
	// override __get, __set and __call
	function __get($sName, &$sValue)
	{
		// start off with no object
		for ($i=0;$i<count($this->_aExtended);$i++) {
			// check it out
			if (property_exists($this->_aExtended[$i], $sName)) {
				$sValue = $this->_aExtended[$i]->$sName;
				return true;
			}
		}
		
		return false;
	}
	
	function __set($sName, &$sValue)
	{
		// loop through objects
		for ($i=0;$i<count($this->_aExtended);$i++) {
			// first try property
			if (property_exists($this->_aExtended[$i], $sName)) {
				$this->_aExtended[$i]->$sName = $sValue;
				return true;
			}
		}
		return false;
	}
	
	function __call($sName, $aArgs = array(), &$return)
	{
		// try method?
		if (method_exists($this, $sName)) {
			$return = call_user_func_array(array($this, $sName), $aArgs);
			return true;
		}
		
		// loop through objects
		for ($i=0;$i<count($this->_aExtended);$i++) {		
			// callable?
			if (method_callable($this->_aExtended[$i], $sName)) {
				$return = call_user_func_array(array(&$this->_aExtended[$i], $sName), $aArgs);
				return true;
			}
		}
		
		// does it exist as _$sName
		if (method_exists($this, '_' . $sName)) {
			$return = call_user_func_array(array($this, '_' . $sName), $aArgs);
			return true;
		}
 
		return false;
	}
}